﻿using System.Text;
using System.Web.Routing;
using System.Web.Mvc.Ajax;

namespace System.Web.Mvc
{
    public static class MikePagerAjaxExtensions
    {
        #region MikePager 分页控件

        public static MvcHtmlString MikePager<T>(this AjaxHelper html, PagedList<T> data)
        {

            int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1;
            int end = (data.TotalPage - start) > 10 ? start + 10 : data.TotalPage;

            RouteValueDictionary vs = html.ViewContext.RouteData.Values;

            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            foreach (string key in queryString.Keys)
                vs[key] = queryString[key];

            var FormString = html.ViewContext.HttpContext.Request.Form;
            foreach (string key in FormString.Keys)
                vs[key] = FormString[key];

            var builder = new StringBuilder();
            builder.AppendFormat("<div class=\"mike_mvc_pager\">");

            //vs["pageSize"] = data.PageSize;
            if (data.IsPreviousPage)
            {
                vs["pageIndex"] = 1;

                //builder.Append(Ajax.AjaxExtensions.ActionLink(html, "|<", vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }));
                builder.Append(html.ActionLink("|<", vs["action"].ToString(), vs, new CustomAjaxOptions()));
                vs["pageIndex"] = data.PageIndex - 1;

                //builder.Append(Ajax.AjaxExtensions.ActionLink(html, "<", vs["action"].ToString(), vs, new Ajax.AjaxOptions() { LoadingElementId = "Loading", UpdateTargetId = "Main" }));
                builder.Append(html.ActionLink("<", vs["action"].ToString(), vs, new CustomAjaxOptions()));

            }

            for (int i = start; i <= end; i++) //前后各显示5个数字页码
            {
                vs["pageIndex"] = i;

                if (i == data.PageIndex)
                {
                    builder.Append("<font class='thispagethis ui-state-default'>" + i.ToString() + "</font>");
                }
                else
                {
                    builder.Append(html.ActionLink(i.ToString(), vs["action"].ToString(), vs));
                }
            }

            if (data.IsNextPage)
            {
                vs["pageIndex"] = data.PageIndex + 1;

                builder.Append(html.ActionLink(">", vs["action"].ToString(), vs));

                vs["pageIndex"] = data.TotalPage;

                builder.Append(html.ActionLink(">|", vs["action"].ToString(), vs));

            }
            builder.Append("每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "页 </div>");
            MvcHtmlString htmlString = new MvcHtmlString(builder.ToString());
            return htmlString;
        }
        #endregion

    }
}
